<?php

namespace App\Services;

use App\Models\StatisticLog;
use App\Jobs\StatisticLogJob;
use App\Repositories\Contracts\CulturalHeritageRepository;
use App\Repositories\Transformers\HomeCulturalHeritageTransformer;

class CulturalHeritageService
{
    private $culturalHeritageRepository;

    public function __construct(CulturalHeritageRepository $culturalHeritageRepository)
    {
        $this->culturalHeritageRepository = $culturalHeritageRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [
                'type' => $param['type'] ?? 1,
            ];
            if (isset($param['keyword'])) {
                $filter['keyword'] = $param['keyword'] ?? '';
            }

            $result = $this->culturalHeritageRepository->paginate($param['per_page'] ?? null, $filter);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->culturalHeritageRepository->find($id);

            if (!empty($result)) {
                //更新点击量
                $params = [
                    'type' => StatisticLog::TYPE_CULTURAL_HERITAGES,
                    'res_id' => $id,
                    'user_id' => request()->user('api')->user_id ?? 0,
                    'user_ip' => get_client_ip(),
                ];
                StatisticLogJob::dispatchNow($params);
            }

            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function home()
    {
        try {
            $filter = [
                'is_home' => 1,
            ];

            $culturalHeritage = $this->culturalHeritageRepository->findByFilter($filter);

            $result = fractal($culturalHeritage, new HomeCulturalHeritageTransformer());

            return $result->toArray();

        } catch (\Throwable $th) {
            throw $th;
        }

    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            $culturalHeritage = $this->culturalHeritageRepository->store($param);
            return $culturalHeritage;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $culturalHeritage = $this->culturalHeritageRepository->update($param, $id);
            return $culturalHeritage;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $culturalHeritage = $this->culturalHeritageRepository->delete($id);

            $result = [];
            if ($culturalHeritage) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
